package edu.zjut.hjg.saas.biz.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.support.AppStatus;
import edu.zjut.hjg.saas.biz.support.Paginated;
import edu.zjut.hjg.saas.biz.util.EscapeUtil;

public class KindergartenDaoImpl extends SqlMapClientDaoSupport implements
        edu.zjut.hjg.saas.biz.dao.KindergartenDao {

    @Override
    public Integer create(Kindergarten kindergarten) {
        return (Integer) getSqlMapClientTemplate().insert("kindergarten.create", kindergarten);
    }

    @Override
    public Kindergarten findBySiteDomain(String siteDomain) {
        return (Kindergarten) getSqlMapClientTemplate().queryForObject(
                "kindergarten.findBySiteDomain", siteDomain);
    }

    @Override
    public Kindergarten load(Integer id) {
        return (Kindergarten) getSqlMapClientTemplate().queryForObject("kindergarten.load", id);
    }

    @Override
    public void update(Kindergarten kindergarten) {
        getSqlMapClientTemplate().update("kindergarten.update", kindergarten);
    }

    @Override
    public Kindergarten findByTenantId(Integer tenantId) {
        return (Kindergarten) getSqlMapClientTemplate().queryForObject(
                "kindergarten.findByTenantId", tenantId);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Kindergarten> listByStatus(AppStatus status, int pageSize, int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject(
                "kindergarten.countByStatus", status.toString());
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        params.put("status", status);
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Kindergarten> list = getSqlMapClientTemplate().queryForList(
                "kindergarten.listByStatus", params);
        Paginated<Kindergarten> paginated = new Paginated<Kindergarten>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Kindergarten> listByCityCodeAndStatus(String cityCode, AppStatus status,
                                                           int pageSize, int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("status", status);
        params.put("cityCode", cityCode);
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject(
                "kindergarten.countByCityCodeAndStatus", params);
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Kindergarten> list = getSqlMapClientTemplate().queryForList(
                "kindergarten.listByCityCodeAndStatus", params);
        Paginated<Kindergarten> paginated = new Paginated<Kindergarten>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Kindergarten> listByProvinceCodeAndStatus(String provinceCode,
                                                               AppStatus status, int pageSize,
                                                               int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("status", status);
        params.put("provinceCode", provinceCode);
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject(
                "kindergarten.countByProvinceCodeAndStatus", params);
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Kindergarten> list = getSqlMapClientTemplate().queryForList(
                "kindergarten.listByProvinceCodeAndStatus", params);
        Paginated<Kindergarten> paginated = new Paginated<Kindergarten>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Kindergarten> listByNameAndStatus(String name, AppStatus status, int pageSize,
                                                       int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("status", status);
        params.put("name", EscapeUtil.sqlLike(name));
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject(
                "kindergarten.countByNameAndStatus", params);
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Kindergarten> list = getSqlMapClientTemplate().queryForList(
                "kindergarten.listByNameAndStatus", params);
        Paginated<Kindergarten> paginated = new Paginated<Kindergarten>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }
}
